// projects/campus/pages/qa/qa.js
const ProjectBiz = require('../../biz/project_biz.js');
const cloudHelper = require('../../../../helper/cloud_helper.js');
const PassportBiz = require('../../../../comm/biz/passport_biz.js');
const WxParse = require('../../../../components/wxParse/wxParse.js');
const Typewriter = require('../../biz/type_index.js');
const a = '# 我是一级标题\n' +
	'## 我是二级标题\n' +
	'### 我是三级标题\n' +
	'[**Showdown**](http://www.showdownjs.com) is *great*\n' +
	'\nbecause:\n' +
	' - it\'s easy to use\n' +
	' - it\'s extensible\n' +
	' - works in the server and in the browser\n' +
	'```sql\n' + `select * from t_ad where id = 1

select ad_name from t_ad where id = 1`+ '\n```\n'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		question: "",
		loading: false,
		interval: null,
		inputValue: "",
		answer: "",
		answerNodes: {},
		getLanguage: (val) => val.split(' ')[0]
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		if (PassportBiz.isLogin()) {
			// let user = {};
			// user.USER_NAME = PassportBiz.getUserName();
			// this.setData({ user });
			this._getUserInfo();
		}
		ProjectBiz.initPage(this);
		// const writer = new Typewriter(a, (text) => {
		// 	this.setData({
		// 		answer: text
		// 	})
		// });
		// setTimeout(() => {
		// 	writer.setText((val)=> val + "这是一条测试数据")
		// }, 1000)
	},
	_handleInput(e) {
		this.setData({
			inputValue: e.detail.value
		})
	},
	async _getUserInfo() {
		let opts = {
			title: 'bar'
		}
		let user = await cloudHelper.callCloudData('passport/my_detail', {}, opts);
		if (!user) {
			this.setData({
				user: null
			});
			return;
		}
		this.setData({
			user
		})
	},
	async _sendQuestion(e) {
    const val = this.data.inputValue;
		const _this = this;

		if (!val || !this.data.inputValue) {
			wx.showToast({
				title: '请输入问题',
				icon: 'none'
			})
			return;
    }
    this.setData({
			inputValue: ""
		})
		if (this.loading) return;
		this.setData({ loading: true });
		new Typewriter(val, (text) => {
			this.setData({
				question: text
			})
		});
		const requestTask = wx.request({
			url: 'http://api.jsonlee.cn:3000/api/openai/stream',
			data: {
				prompt: val
			},
			enableChunked: true,
			success(res) {
				console.log("请求结束", res);
			},
			fail(err) {
				console.error("请求失败", err);
				_this.setData({
					inputValue: val,
					question: "",
					answer: ""
				})
			}
		})
		const answerWriter = new Typewriter("", (text) => {
			// console.log(text, ">>>");
			this.setData({
				answer: text
			})
		});
		requestTask.onChunkReceived((response) => {
			// console.log('response', response)
			const decoder = new TextDecoder('utf-8');
			const responseText = decoder.decode(response.data);
			answerWriter.setText(pre=> pre + responseText)
			// _this.setData({
			// 	answer: this.data.answer + responseText
			// })
		});
		// console.log(this);
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})
